package net.zhangbaolin.systemadmin.controller.system;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.zhangbaolin.systemadmin.entity.Menu;
import net.zhangbaolin.systemadmin.entity.Permission;
import net.zhangbaolin.systemadmin.service.PermissionService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * 菜单管理
 * @author zhangbaolin
 * @since 2023-09-14 15:22:21
 */
@RestController
@RequestMapping("menu")
public class MenuController {
    /**
     * 服务对象
     */
    @Resource
    private PermissionService permissionService;

    /**
     * 返回权限列表
     *
     * @param page 分页对象
     * @param permission 查询实体
     * @return 所有数据
     */
    @GetMapping
    @PreAuthorize("hasAuthority('system:menu:list')")
    public List<Permission> selectAll() {
        //获取SecurityContextHolder中的用户权限标识
        SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        return permissionService.list();
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @PreAuthorize("hasAuthority('system:menu:query')")
    public Permission selectOne(@PathVariable Serializable id) {
        return this.permissionService.getById(id);
    }

    /**
     * 新增数据
     *
     * @param permission 实体对象
     * @return 新增结果
     */
    @PostMapping
    @PreAuthorize("hasAuthority('system:menu:add')")
    public boolean insert(@RequestBody Permission permission) {
        return this.permissionService.save(permission);
    }

    /**
     * 修改数据
     *
     * @param permission 实体对象
     * @return 修改结果
     */
    @PutMapping
    @PreAuthorize("hasAuthority('system:menu:edit')")
    public boolean update(@RequestBody Permission permission) {
        return this.permissionService.updateById(permission);
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    @PreAuthorize("hasAuthority('system:menu:delete')")
    public boolean delete(@RequestParam("idList") List<Long> idList) {
        return this.permissionService.removeByIds(idList);
    }
}

